import { Context } from 'egg';

module.exports = () => {
    return async function header(ctx: Context, next) {

        const uuid = ctx.headers?.shopuuid;
        if(!uuid) {
            ctx.badRequest({});
            return;
        }

        let shop_id = null;
        shop_id = await ctx.service.cache.get(`shop_id_${uuid}`);
        console.log(shop_id, 'shop_id')
        if (!shop_id) {
            const shop = await ctx.model.Shop.findOne({ where: { uuid } });
            shop_id = shop.id;
            ctx.service.cache.set(`shop_id_${uuid}`, shop_id, '')
        }

        ctx._shop = {
            id: shop_id
        };
        await next();
    }
}
